package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dkd.common.constant.DkdContants;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.bean.BeanUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.domain.vo.VendingMachineVo;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author itheima
 * @date 2025-01-22
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //设备编号
        // 为售货机生成唯一的内部代码
        vendingMachine.setInnerCode(UUIDUtils.getUUID());

        // 根据售货机的类型ID获取售货机类型信息
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());

        // 将售货机类型的通道最大容量设置到售货机对象中
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());

        // 根据节点ID获取节点信息
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());

        // 将节点的相关属性复制到售货机对象中，但不包括ID属性
        BeanUtils.copyProperties(node,vendingMachine,"id");

        // 设置售货机的初始状态为未部署
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);

        // 设置售货机的地址信息
        vendingMachine.setAddr(node.getAddressDetail());

        // 设置售货机的更新时间和创建时间为当前日期
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        vendingMachine.setCreateTime(DateUtils.getNowDate());

        // 将售货机信息插入到数据库中
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        List<Channel> channels = new ArrayList<>();
        for (int i = 1; i <= vmType.getVmRow();i++){
            for (int j = 1; j <= vmType.getVmCol();j++){
                Channel channel = new Channel();
                // 设置售货机的通道编号
                channel.setChannelCode(i+"-"+j);
               channel.setVmId(vendingMachine.getId());
               channel.setInnerCode(vendingMachine.getInnerCode());
               channel.setMaxCapacity(vmType.getChannelMaxCapacity());
               channel.setCreateTime(DateUtils.getNowDate());
               channel.setUpdateTime(DateUtils.getNowDate());
               channels.add(channel);
            }
        }
channelMapper.insertChannelList(channels);

        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)

    {
        if (vendingMachine.getNodeId()!=null) {
            Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
            BeanUtils.copyProperties(node,vendingMachine,"id");
            vendingMachine.setAddr(node.getAddressDetail());
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    @Override
    public List<VendingMachineVo> selectVendingMachineListVo(VendingMachine vendingMachine) {
        return vendingMachineMapper.selectVendingMachineListVo(vendingMachine);
    }

    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }
}
